package com.learn.algorlthm;

/**
 * @classname:
 * @Description:树的子结构
 * @author: yfpu(fengpu.yang @ ucarinc.com)
 * @Date: 2018-07-28 15:15
 * 题目描述：
 *  输入两棵二叉树A，B，判断B是不是A的子结构。（ps：我们约定空树不是任意一个树的子结构）
 */
public class Learn26 {

    public static boolean hasSubtree(TreeNode root1, TreeNode root2) {
        if (root1 == null)
            return false;
        if (root2 == null)
            return false;

        return isBubtreeWithRoot(root1, root2) || hasSubtree(root1.left, root2) || hasSubtree(root1.right, root2);
    }

    private static boolean isBubtreeWithRoot(TreeNode root1, TreeNode root2) {
        if (root1 == null)
            return false;
        if (root2 == null)
            return false;
        if (root1.val == root2.val)
            return true;
        return isBubtreeWithRoot(root1.left, root2.left) && isBubtreeWithRoot(root1.right, root2.right);
    }




    class TreeNode {
        public int val;
        public TreeNode left;
        public TreeNode right;
    }

}
